package com.mfoyou.agent.web.utils.withdraw;

import java.util.LinkedList;
import java.util.List;

import org.mfoyou.agent.utils.common.JsonRet;

public abstract class AbstractWithDraw implements IWithDraw {

    private String message;

    private List<WithDrawListener> listenerList = new LinkedList<WithDrawListener>();

    @Override
    public void pre() {

    }

    @Override
    public JsonRet<?> done() {
        pre();
        return dealWith() ? success() : fail();
    }

    @Override
    public final JsonRet<?> success() {
        for (WithDrawListener listener : listenerList) {
            listener.success(message);
        }
        return JsonRet.buildRet(0);
    }

    @Override
    public final JsonRet<?> fail() {
        for (WithDrawListener listener : listenerList) {
            listener.fail(message);
        }
        return JsonRet.buildFailRet(message);
    }

    @Override
    public abstract boolean dealWith();

    public void addListener(WithDrawListener withDrawListener) {
        this.listenerList.add(withDrawListener);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}
